package com.cssw.fyzb.busi.api.acct;

import com.cssw.fyzb.busi.api.user.GetUserInfo;
import com.cssw.fyzb.busi.service.acct.AcctService;
import com.cssw.fyzb.busi.service.system.SysParamService;
import com.cssw.fyzb.busi.service.team.TeamService;
import com.cssw.fyzb.busi.service.user.UserExtService;
import com.cssw.fyzb.busi.service.user.UserService;
import com.cssw.fyzb.core.model.dodb.*;
import com.cssw.fyzb.core.model.vo.acct.AcctGlobalVo;
import com.cssw.fyzb.core.model.vo.acct.AcctGzGlobalVo;
import com.cssw.fyzb.core.util.TimeUtil;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;


/**
 * 获取用户钱包信息
 * <p>
 * asa
 */
@Function(description = "获取用户果子信息", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true)
})
@Component
public class GetGzAcctInfo implements Action {

    @Autowired
    private AcctService acctService;

    @Autowired
    private TeamService teamService;

    @Autowired
    private UserService userService;

    //新老用户注册时间规则
    @Value("${task.limitDate:2021-01-21 00:00:00}")
    private String limitDateStr;

    @Autowired
    private SysParamService sysParamService;
    @Autowired
    private UserExtService userExtService;

    private static final Logger logger = LoggerFactory.getLogger(GetUserInfo.class);

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");

        try {
            User user = userService.getById(userId);

            Acct acct = acctService.queryAcctByUIdAndType(userId, AcctType.ACCT_TYPE_G);
            AcctGzGlobalVo vo = new AcctGzGlobalVo();
            vo.setBalance(acct.getBalanceFee().setScale(4, BigDecimal.ROUND_DOWN));

            if (isNewUser(user)) {
                vo.setNewUser("1");
            } else {
                vo.setNewUser("0");
            }

            //新老用户走同一个逻辑
            Integer teamNum = teamService.getTeamNum(userId);
            vo.setTeamNum(teamNum);

            BigDecimal gzAddPrice = userExtService.getGzAddPrice(userId);
            BigDecimal gzPrice = null;
            TeamGzPrice teamGzPrice = acctService.getTeamGzPriceByNum(teamNum);
            if (null == teamGzPrice) {
                gzPrice = BigDecimal.ZERO.add(gzAddPrice);
                vo.setPrice(gzPrice.setScale(4, BigDecimal.ROUND_DOWN));
                vo.setTotalPrice(acct.getBalanceFee().multiply(gzPrice).setScale(2, BigDecimal.ROUND_DOWN));
                vo.setMonthLimitNum(BigDecimal.ZERO);
            } else {
                gzPrice = teamGzPrice.getPrice().add(gzAddPrice);
                vo.setPrice(gzPrice.setScale(4, BigDecimal.ROUND_DOWN));
                vo.setTotalPrice(acct.getBalanceFee().multiply(gzPrice).setScale(2, BigDecimal.ROUND_DOWN));
                vo.setMonthLimitNum(teamGzPrice.getMonthLimitNum());
            }

            return Response.success().put("data", vo);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("操作异常");
        }
    }

    private boolean isNewUser(User user) {
        //判断是否新用户
        Date limitDate = TimeUtil.parse(this.limitDateStr, "yyyy-MM-dd HH:mm:ss");
        if (null != limitDate && null != user.getCreateTime() && user.getCreateTime().before(limitDate)) {
            return false;
        }
        return true;
    }
}
